﻿#include "MultiVTKOutput.h"
#include "MultiSystem.h"
#include "System.h"
#include "PassFlow.h"
#include "Timer.h"
#include "Transient.h"

#include <fmt/format.h>
#include <vtkm/Version.h>
#include <vtkm/io/VTKDataSetWriter.h>

RegisterObject(MultiVTKOutput);
MultiVTKOutput::MultiVTKOutput(const Configuration& cfg)
  : VTKOutput(cfg)
{
}

void MultiVTKOutput::Execute()
{
  Timer timer;
  timer.Start();
  if (ShouldOutput())
  {
    try
    {
      MultiSystem& multi_sys = const_cast<MultiSystem&>(dynamic_cast<const MultiSystem&>(_system));
      auto& structured_sys = multi_sys.GetStructuredSystem();
      auto& unstructured_sys = multi_sys.GetUnStructuredSystem();
      try
      {
        {
          auto current_step = _executioner.CurrentStep();
          std::string file_name =
            fmt::format("{0}_{1:0>5}.{2}", _file_base + "1", current_step, "vtk");

          vtkm::io::VTKDataSetWriter writer(file_name);
          writer.SetFileTypeToBinary();
          writer.WriteDataSet(structured_sys->GetDateSet());
        }

        {
          auto current_step = _executioner.CurrentStep();
          std::string file_name =
            fmt::format("{0}_{1:0>5}.{2}", _file_base + "2", current_step, "vtk");

          vtkm::io::VTKDataSetWriter writer(file_name);
          writer.SetFileTypeToBinary();
          writer.WriteDataSet(unstructured_sys->GetDateSet());
        }
      }
      catch (const std::exception& e)
      {
        Error(e.what());
      }
    }
    catch (const std::exception&)
    {
      Error("System 不是 MultiSystem的子类");
    }


    Success("MultiVTKOutput::Execute, Time Elapsed: ", timer.GetElapsedTime());
  }
}
